package com.example.controller;

import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.example.model.User;
import com.example.util.ExcelUtil;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@RestController
public class TestController {
	
	@Value("${testValue}")
	private String testValue;
	
	private String test = "66";
	
	@ApiOperation(value="valueTest1", notes="notesTest22")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "User")
    })
	@RequestMapping(value = "/testIndById",method= RequestMethod.POST)
	@ResponseBody
	public User findAll(Integer id){
		return null;
	}
	
	CountDownLatch doneSignal = new CountDownLatch(3);
	
	@RequestMapping(value = "/a",method= RequestMethod.GET)
	@ResponseBody
	public void getString(){
		doneSignal.countDown();
	}
	
	@RequestMapping(value = "/b",method= RequestMethod.GET)
	@ResponseBody
	public void setString(String str) throws InterruptedException{
		doneSignal.await();
		System.out.println("XXXXXXXXXXXXXXXXXXXXXXXXXXX");
	}
	
	@RequestMapping(value = "/get",method= RequestMethod.GET)
	@ResponseBody
	public void getDate( HttpServletResponse response){
		long startTime = System.currentTimeMillis(); // 获取开始时间  
		OutputStream  bos = null;
		try {
			List<String> listTitle = new ArrayList<>();
			listTitle.add("名称");
			listTitle.add("年龄");
			
			List<Map<String, String>> listMap = new ArrayList<>();
			for (int i = 0; i < 10000; i++) {
				Map<String, String> map = new HashMap<>();
				map.put("name", "张三李四王五赵六孙七");
				map.put("age", ( i + 1 ) + "");
				listMap.add(map);
			}
			//生成word的流
			byte[] dat = ExcelUtil.excelDownloadc(listTitle, listMap);
			
			String fileName = "测试";
			response.setContentType("application/msexcel");
			response.setHeader("Content-Disposition", "attachment;Filename=" +URLEncoder.encode(fileName, "UTF-8")+".xlsx");
			bos = response.getOutputStream();
			bos.write(dat);
			bos.flush();
		} catch (Exception e) {
		} catch (Throwable e) {
		}finally{
			try {
				if (bos != null) {
					bos.close();
				}
			} catch (IOException e) {
			}
		}
		long endTime = System.currentTimeMillis(); // 获取结束时间  
        System.out.println("程序运行时间： " + (endTime - startTime) + "ms");  
	}
}
